package hu;

import java.util.*;

public class min_trans_time {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        List<List<int[]>> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int v = sc.nextInt();
            list.get(a - 1).add(new int[]{b ,v});
        }
        int begin = sc.nextInt();
        int end = sc.nextInt();
        int min_time = Integer.MAX_VALUE;
        for (int i = 0; i < list.get(begin - 1).size(); i++) {
            if(list.get(begin - 1).get(i)[0] == end)
                min_time = Math.min(min_time, list.get(begin - 1).get(i)[1]);
            else
                min_time = Math.min(min_time, dfs(list, list.get(begin - 1).get(i)[0],
                        list.get(begin - 1).get(i)[1], end));
        }
        if(min_time == Integer.MAX_VALUE)
            System.out.println("-1");
        else
            System.out.println(min_time);
    }
    public static int dfs(List<List<int[]>> list, int index, int sum, int end){
        for (int i = 0; i < list.get(index - 1).size(); i++) {
            if(list.get(index - 1).get(i)[0] == end)
                return sum + list.get(index - 1).get(i)[1];
            else
                return dfs(list, list.get(index - 1).get(i)[0], sum + list.get(index - 1).get(i)[1], end);
        }
        return Integer.MAX_VALUE;
    }
}
/*
3 3
1 2 11
2 3 13
1 3 50
1 3


4 5
1 2 10
2 4 5
1 4 50
1 3 3
3 2 2
4 1
 */